<template>
  <a-card :bordered="false">
    <div class="table-operator">
      <a-row :gutter="10">
        <a-col :md="20" :sm="24">
          <a-button
            v-if="hasPerm('PB_MaterialType.Add')"
            type="primary"
            icon="plus"
            @click="hanldleAdd()"
          >新建</a-button
          >
          <a-button
            v-if="hasPerm('PB_MaterialType.Delete')"
            type="primary"
            icon="minus"
            @click="handleDelete(selectedRowKeys)"
            :disabled="!hasSelected()"
            :loading="loading"
          >删除</a-button
          >
          <a-button type="primary" icon="redo" @click="react">刷新</a-button>
        </a-col>
        <a-button v-if="hasPerm('PB_MaterialType.Leading')" type="primary" @click="hanldleLeading()">导入数据</a-button>
      </a-row>
    </div>

    <a-table
      ref="table"
      :scroll="{ y: 560 }"
      :columns="columns"
      :rowKey="row => row.Id"
      :dataSource="data"
      :pagination="pagination"
      :loading="loading"
      @change="handleTableChange"
      @expand="handleTableExpand"
      :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
      :bordered="true"
      size="small"
    >
      <span slot="action" slot-scope="text, record">
        <template>
          <a v-if="hasPerm('PB_MaterialType.Edit')" @click="handleEdit(record.Id)">编辑</a>
          <a-divider v-if="hasPerm('PB_MaterialType.Delete')" type="vertical" />
          <a v-if="hasPerm('PB_MaterialType.Delete')" @click="handleDelete([record.Id])">删除</a>
        </template>
      </span>
    </a-table>

    <edit-form ref="editForm" :parentObj="this"></edit-form>
    <leading-show
      ref="leadingShow"
      :parentObj="this"
      leading="/PB/PB_MaterialType/Import"
      templet="/PB/PB_MaterialType/ExportToExcel"
    ></leading-show>
  </a-card>
</template>

<script>
import EditForm from './EditForm'
import LeadingShow from '../../../components/PB/LeadingShow2'

const columns = [
  // {
  //   title: '序号',
  //   dataIndex: 'aId',
  //   customRender: (text, row, index) => {
  //     return index + 1
  //   },
  //   width: '50px'
  // },
  { title: '分类编码', dataIndex: 'Code' },
  { title: '分类名称', dataIndex: 'title' },
  { title: '备注', dataIndex: 'Remarks' },
  { title: '操作', dataIndex: 'action', scopedSlots: { customRender: 'action' }, align: 'center' }
]

export default {
  components: {
    EditForm,
    LeadingShow
  },
  created () {
    this.getDataList()
  },
  data () {
    return {
      data: [],
      pagination: {
        current: 1,
        pageSize: 25,
        showTotal: (total, range) => `总数:${total} 当前:${range[0]}-${range[1]}`,
        pageSizeOptions: ['25', '50', '100'], // 设置pageSize的可选值，页面啊可以通过下拉框进行选择
        showSizeChanger: true,
        onShowSizeChange: (current, pageSize) => (this.pageSize = pageSize)
      },
      filters: {},
      sorter: { field: 'Code', order: 'asc' },
      loading: false,
      columns,
      queryParam: {},
      selectedRowKeys: []
    }
  },
  methods: {
    react () {
      this.data = []
      this.getDataList()
    },
    handleTableExpand (expanded, record) {
      console.log(expanded, record)
      if (expanded) {
        if (record.children.length) {
          return
        }
        this.loading = true
        this.$http.post('/PB/PB_MaterialType/GetMaterialTypeList?ParentId=' + record.Id, {}).then(res => {
          res.Data.forEach(item => {
            this.loading = false

            if (item.isLeaf) {
              this.$set(item, 'children', null)
            } else {
              this.$set(item, 'children', [])
            }
          })
          record.children = res.Data
        })
      }
    },
    handleTableChange (pagination, filters, sorter) {
      this.pagination = { ...pagination }
      this.filters = { ...filters }
      this.sorter = { ...sorter }
      this.getDataList()
    },
    getDataList (id = '') {
      this.selectedRowKeys = []

      this.loading = true
      this.$http
        .post('/PB/PB_MaterialType/GetMaterialTypeList?ParentId=' + id, {
          PageIndex: this.pagination.current,
          PageRows: this.pagination.pageSize,
          SortField: this.sorter.field || 'Id',
          SortType: this.sorter.order,
          Search: this.queryParam,
          ...this.filters
        })
        .then(resJson => {
          this.loading = false

          if (resJson.Total) {
            const pagination = { ...this.pagination }
            pagination.total = resJson.Total
            this.pagination = pagination
          }
          resJson.Data.forEach(item => {
            if (item.isLeaf) {
              item.children = null
            } else {
              item.children = []
            }
          })
          this.data = resJson.Data
        })
    },
    onSelectChange (selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },
    hasSelected () {
      return this.selectedRowKeys.length > 0
    },
    hanldleAdd () {
      this.$refs.editForm.openForm(null, '新建物品类型')
    },
    handleEdit (id) {
      this.$refs.editForm.openForm(id, '编辑物品类型')
    },
    hanldleLeading () {
      this.$refs.leadingShow.openForm(null, '导入物品类型')
    },
    handleDelete (ids) {
      this.$confirm('确认删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        return new Promise((resolve, reject) => {
          this.$http.post('/PB/PB_MaterialType/DeleteData', ids).then(resJson => {
            resolve()

            if (resJson.Success) {
              this.$message.success('操作成功!')

              this.getDataList()
            } else {
              this.$message.error(resJson.Msg)
            }
          })
        }).catch(() => console.log('Oops errors!'))
      })
    }
  }
}
</script>
